﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using OrganizedIN.BAL;
using OrganizedIN.BussinessServices;

namespace OrganizedIN.UI.Media
{
    public partial class AddNewMedia : System.Web.UI.Page
    {
        BLL oibll = new BLL();
        DateTime dt = DateTime.Now;
        static FileUpload takeMedia1 = new FileUpload();
         MyProfileMedia objMedia = new MyProfileMedia();

        public static string MEDIAID = "";
        public static string path = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            lblTodayDate.Text = String.Format("{0:D}", dt);
            getID();

        }

        public string getID()
        { 
             DataSet ds = oibll.Media_FetchMedia_ID();
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0][0].ToString() != "")
                {
                    MEDIAID = ds.Tables[0].Rows[0][0].ToString();
                    int ID =Convert.ToInt32(MEDIAID);
                    ID++;

                    MEDIAID = ID.ToString();


                }
                else
                {
                    MEDIAID = "1";
                }
            }
            return MEDIAID;
        }
        protected void btn_Save_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_MediaNAme.Text.Trim() != "")
                {
                    string exe;
                    takeMedia1 = FileUpload_Media;
                    if (takeMedia1.HasFile)
                    {
                        exe = "";
                        exe = System.IO.Path.GetExtension(takeMedia1.FileName).ToLower();
                        string[] ar = { ".avi", ".wmv", ".mpeg", ".mpg", ".mov", ".mp4", ".txt", ".bmp", ".gif", ".jpg", ".jpeg" };
                        for (int i = 0; i < ar.Length; i++)
                        {
                            if (exe == ar[i])
                            {
                                FileUpload_Media.SaveAs(Server.MapPath("~/MediaUpload/" + FileUpload_Media.FileName));
                                path = "~/MediaUpload/" + FileUpload_Media.FileName;
                                break;
                            }                            
                        }
                    }

                    objMedia.MediaID = MEDIAID;
                    objMedia.Media_Media_Name = txt_MediaNAme.Text.Trim();
                    objMedia.MediaSummary = txt_Summary.Text.Trim();
                    objMedia.MediaSubscriptionDetail = txt_Subscribe.Text.Trim();
                    objMedia.MediaRating = ddl_Rating.Text.Trim();
                    objMedia.MediaStatus = "";
                    objMedia.MediaSharerName = txt_Share.Text.Trim();
                    objMedia.MediaUserName = Session["UserID"].ToString();
                    objMedia.MediaOfflinePath = path;
                    objMedia.Media_OnlineURL_ID = "";
                    objMedia.Media_CreateDate = String.Format("{0:d/M/yyyy}", dt);
                    oibll.MediaInsert(objMedia);

                    txt_Bookmark.Text = string.Empty;
                    txt_Share.Text = string.Empty;
                    txt_Subscribe.Text = string.Empty;
                    txt_Summary.Text = string.Empty;
                    txt_MediaNAme.Text = string.Empty;

                    gvDocList.DataSource = null;
                    gvDocList.DataBind();
                    getID();
                 }

            }
            catch (Exception ex)
            {

            }

        }

        protected void btn_Import_Click(object sender, EventArgs e)
        {
            FileUpload_Media.SaveAs(Server.MapPath("~/MediaUpload/" + FileUpload_Media.FileName));
            path = "~/MediaUpload/" + FileUpload_Media.FileName;
        }

        protected void btn_Bookmark_Click(object sender, EventArgs e)
        {
            objMedia.MediaID = MEDIAID;
            objMedia.MediaOnlinePath = txt_Bookmark.Text.Trim();
            oibll.AddMediaOnline_Insert(objMedia);
            Bindgrid(MEDIAID);
        }

        public void Bindgrid(string MEDIAID)
        {
            DataSet ds = oibll.Media_FetchBookmark(MEDIAID);
            if (ds.Tables[0].Rows.Count > 0)
            {
                gvDocList.DataSource = ds.Tables[0];
                gvDocList.DataBind();
            }
        
        }
      
    }
}
